package org.lim.shared.websocket.disruptor;

import com.lmax.disruptor.WorkHandler;
import lombok.extern.slf4j.Slf4j;
import org.lim.shared.websocket.model.PictureEditRequestMessage;
import org.lim.shared.websocket.service.PictureEditMessageService;
import org.lim.domain.user.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

/**
 * Disruptor 消费者
 */
@Slf4j
@Component
public class PictureEditEventWorkHandler implements WorkHandler<PictureEditEvent> {

    @Resource
    private PictureEditMessageService messageService;

    @Override
    public void onEvent(PictureEditEvent event) throws Exception {
        try {
            PictureEditRequestMessage requestMessage = event.getPictureEditRequestMessage();
            WebSocketSession session = event.getSession();
            User user = event.getUser();
            Long pictureId = event.getPictureId();

            // 委托给消息服务处理，使用统一的策略模式
            messageService.handleMessage(requestMessage, session, user, pictureId);

        } catch (Exception e) {
            log.error("处理 Disruptor 事件时发生异常", e);
        }
    }
}